$(function () {
    let markid = getUrlString('markid'),show = 0;
    function getInfo () {
        let name = $('#name').val();
        let phone = $('#phone').val();
        let addressDetail = $('#addressDetail').val();
        let options1 = $("#local1 option:selected").val(); //获取选中的项
        let options2 = $("#local2 option:selected").val(); //获取选中的项
        let options3 = $("#local3 option:selected").val(); //获取选中的项
        let address = options1+options2+options3+addressDetail;
        let num = $('#num').val();
        let source = $('#source').val();
        let QQCode = $('#QQCode').val();
        let WXCode = $('#WXCode').val();
        let remark = $('#remark').val();
        if (name === ''){
            laymsg('请输入姓名');
            return
        }
        if (!phoneNumberVer(phone)){
            return;
        }

        if (options1 === ''){
            laymsg('请选择省');
            return
        }

        if (options2 === ''){
            laymsg('请选择市');
            return
        }

        // if (options3 === ''){
        //     laymsg('请选择区');
        //     return
        // }

        if (addressDetail === ''){
            laymsg('请输入详细地址');
            return
        }

        if (num === ''){
            laymsg('请输入商品数量');
            return
        }

        if (num > 12){
            laymsg('活动期间没订单仅限12个商品');
            return
        }

        if (source === ''){
            laymsg('请输入商品来源');
            return
        }

        //所有图片
        let my_imgs = $("#picList").find(".image");
        if (my_imgs.length === 0){
            laymsg('请上传订单商品截图');
            return
        }
        let picurls="";
        my_imgs.each(function(index,e){
            if(picurls === ""){
                picurls=""+$(e).attr("src");
            }else{
                picurls = picurls+";"+$(e).attr("src");
            }
        });
        let data = {
            'markid':markid,
            'consignee':name,
            'phone':phone,
            'address':address,
            'goods_pic':picurls,
            'goods_num':num,
            'source':source,
            'intro':remark,
            'qq_number':QQCode,
            'wx_number':WXCode,
        };
        console.log(data);
        getData('post','/niu/apiv4/niu_user_eneroll.php',data,function (data) {
            console.log(data);
            if (data.resultcode === '0'){
                laymsg(data.msg);
                setTimeout(function () {
                    nativeCommand('dancool://activity=reflashfin');
                },1000)
            } else {
                laymsg(data.msg)
            }
        })
    }

    //同意协议
    $('.notyjsxy').click(function () {
        $(this).hide();
        $('.tyjsxy').show();
        show = 1;
        $('.indexAlert').show();
    });
    //不同意协议
    $('.tyjsxy').click(function () {
        show = 0;
        $(this).hide();
        $('.notyjsxy').show();
    });

    $('#getInfo').one("click",function(){
        if (show === 0){
            laymsg('请同意蛋酱咸蛋寄售协议');
            return
        }
        getInfo()
    });

    $('.upLoad').click(function () {
        uploadpics();
    });

    //同意寄售须知
    $('.notyjsxz').click(function () {
        $(this).hide();
        $(this).next().show();
        $(this).parent().attr('data-jsshow','1');
        let arr = [];
        $('.alertContent').find('.ty').each(function () {
            arr.push($(this).attr('data-jsshow'))
        });
        if (arr.indexOf('0') === -1){
            $('.quren').removeClass('notclick').addClass('qurenBtn');
        }
    });
    //不同意寄售须知
    $('.tyjsxz').click(function () {
        $(this).hide();
        $(this).prev().show();
        $(this).parent().attr('data-jsshow','0');
        $('.quren').addClass('notclick').removeClass('qurenBtn')
    });

    $('.quren').click(function () {
        $('.indexAlert').hide()
    });

    function uploadpics() {
        //查看已经有的图片数，超过9个不再上传
        var size=$("#picList").children().length;
        var left=10 - size;
        //如果有N小于9个，则可一上传9-N个
        //调用本地接口
        // console.log("left uploadimg=" + left);
        if(left>0){
            getnative("dancool://uploadfilev2="+left+";700");
        }else {
            laymsg('最多上传9张图片')
        }
    }
});

function getnative(actionurl) {
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isiOS) {
        window.webkit.messageHandlers.getnative.postMessage(actionurl);
    }else {
        ikags.getnative(actionurl)
    }
}

function jsnativeresult (aaa,bbb,ccc) {
    if (ccc !== '' || ccc !== null){
        let cccArr = ccc.split(';');
        let text = '';
        for (let i = 0;i < cccArr.length;i++){
            text += '<li>' +
                '            <img class="image" src="'+cccArr[i]+'" alt="">' +
                '            <img class="closeAlert" src="../../res/img/common/delete.png" alt="">' +
                '        </li>';
        }

        $('#picList').prepend(text);
        let size2 = $("#picList").children().length;
        if (size2 >= 10){
            $('#addPic').hide();
        }else {
            $('#addPic').show();
        }
        $('#picList li .closeAlert').click(function () {
            $(this).parent().remove();
            let size = $("#picList").children().length;
            if (size < 10){
                $('#addPic').show();
            }
        })
    }

}
